<template>
  <div class="chatbot-container">
    <div class="chat-window">
      <div v-for="(msg, idx) in messages" :key="idx" :class="['chat-message', msg.from]">
        <span class="sender">{{ msg.from === 'user' ? '我' : '团团' }}：</span>
        <span class="text">{{ msg.text }}</span>
      </div>
    </div>
    <div class="chat-input">
      <input
        v-model="input"
        @keyup.enter="sendMessage"
        type="text"
        placeholder="请输入您的问题..."
      />
      <button @click="sendMessage">发送</button>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data() {
    return {
      messages: [
        { from: 'bot', text: '您好，我是你的专属助手团团，有什么可以帮您？' }
      ],
      input: ''
    }
  },
  methods: {
    sendMessage() {
      if (!this.input.trim()) return
      this.messages.push({ from: 'user', text: this.input })
      this.input = ''
      axios.get('http://localhost:8080/ai/test')
        .then(res => {
          console.log(JSON.stringify(res.data.data))
          const cleanText = res.data.data.replace(/<[^>]+>/g, '');
          this.messages.push({ from: 'bot', text: cleanText })
     })
    }
  }
}
</script>

<style scoped>
.chatbot-container {
  width: 350px;
  margin: 40px auto;
  border: 1px solid #e0e0e0;
  border-radius: 10px;
  box-shadow: 0 2px 8px #eee;
  background: #fff;
  display: flex;
  flex-direction: column;
  height: 500px;
}
.chat-window {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
  background: #f9f9f9;
}
.chat-message {
  margin-bottom: 12px;
  display: flex;
  align-items: flex-start;
}
.chat-message.user {
  justify-content: flex-end;
}
.chat-message.bot {
  justify-content: flex-start;
}
.sender {
  font-weight: bold;
  margin-right: 6px;
  color: #409eff;
}
.text {
  background: #e6f7ff;
  padding: 6px 12px;
  border-radius: 16px;
  max-width: 70%;
  word-break: break-all;
}
.chat-message.user .text {
  background: #d1ffd6;
  color: #333;
}
.chat-input {
  display: flex;
  border-top: 1px solid #eee;
  padding: 10px;
  background: #fafafa;
}
.chat-input input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #ccc;
  border-radius: 16px;
  outline: none;
  margin-right: 8px;
}
.chat-input button {
  background: #409eff;
  color: #fff;
  border: none;
  border-radius: 16px;
  padding: 8px 18px;
  cursor: pointer;
  transition: background 0.2s;
}
.chat-input button:hover {
  background: #3076c9;
}
</style> 